<template>
  <div class="goodinfo">
    <transition @before-enter="before" @enter="enter" @after-enter="leave">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <lunbotu :lunbolist="Piclist" :isfull="false"></lunbotu>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">{{Goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{Goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{Goodsinfo.sell_price}}</span>
          </p>

          <p>
            购买数量：
            <numbtn @getcount="getCount" :maxcount="Goodsinfo.stock_quantity"></numbtn>
          </p>

          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{Goodsinfo.goods_no}}</p>
          <p>库存情况：{{Goodsinfo.stock_quantity}}</p>
          <p>上架时间：{{Goodsinfo.add_time | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="GotoIntroduce(Goodsinfo.id)">图文介绍</mt-button>
        <!-- <br /> -->
        <mt-button type="danger" size="large" plain @click="Gotosay(Goodsinfo.id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import lunbotu from "../Lunbotu.vue";
import numbtn from "../numbtn.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      Piclist: [],
      Goodsinfo: [],
      ballflag: false,
      selectcount: 1
    };
  },
  created() {
    this.getGoodsinfo();
    this.getGoodsPrice();
  },
  methods: {
    getGoodsinfo() {
      this.$http
        .get("http://www.liulongbin.top:3005/api/getthumimages/" + this.id)
        .then(result => {
          if (result.body.status === 0) {
            result.body.message.forEach((item, i) => {
              item.img = item.src;
            });
            this.Piclist = result.body.message;
          }
        });
    },
    getGoodsPrice() {
      this.$http
        .get("http://www.liulongbin.top:3005/api/goods/getinfo/" + this.id)
        .then(result => {
          if (result.body.status === 0) {
            this.Goodsinfo = result.body.message[0];
          }
        });
    },
    GotoIntroduce(id) {
      this.$router.push({ name: "goodsintroduce", params: { id } });
    },
    Gotosay(id) {
      this.$router.push({ name: "goodscomments", params: { id } });
    },
    addShopCar() {
      this.ballflag = !this.ballflag;

      var shopinfo = {
        id: this.id,
        select: true,
        price: this.Goodsinfo.sell_price,
        number: this.selectcount
      };
      this.$store.commit("AddtoshopCar", shopinfo);
    },
    before(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;

      const balldistance = this.$refs.ball.getBoundingClientRect();
      const flagdistance = document
        .getElementById("Carbadge")
        .getBoundingClientRect();
      const xDis = flagdistance.left - balldistance.left;
      const yDis = flagdistance.top - balldistance.top;
      el.style.transform = "translate(" + xDis + "px," + yDis + "px)";
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    leave(el) {
      this.ballflag = !this.ballflag;
    },
    getCount(count) {
      this.selectcount = count;
    }
  },
  components: {
    lunbotu,
    numbtn
  }
};
</script>

<style lang="scss" scoped>
.goodinfo {
  background-color: #eeeeee;
  overflow: hidden;
  .mui-card-content-inner {
    .now_price {
      font-size: 15px;
      color: red;
    }
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 5px;
    }
  }
  .ball {
    background-color: red;
    border-radius: 50%;
    width: 10px;
    height: 10px;
    position: absolute;
    z-index: 100;
    top: 390px;
    left: 146px;
  }
}
</style>